<?php

namespace Admin\Controller; //当前命名空间跟文件目录一样
use Common\Common\CommonController;  //继承公共控制器类，时刻检测管理员身份
class GoodsController extends CommonController
{
	//显示所有商品
	public function index()
	{
		//检查管理员登录信息
		$this->checkAdmin();
		//实例化分类控制器对象，调用其显示分类的show()方法，获取分类信息
		$categoryInfo = R('Home/Category/show');
		//调用模型类的category()方法，解析字符串，得到理想的分类信息数组
		$model = D('Goods');
		$info = $model->category($categoryInfo); //重新组合分类数组
		$this->assign('categoryInfo', $info);  //返回重新组合分类数组,到视图中去显示分页列表

		//接收get传递的商品分类编号ci，默认为空，显示最新上架商品
		$ci = I('get.ci') != null ? I('get.ci') : null; //没有传递默认为空

		//所以总结出来传递page和array('category_id'=>3)给模型分页方法,然后返回处理后的当前页page和总页数pages和limit字符
		//定义当前页码因为页码是用户点击出现的(get方法获得)，用户第一次进来没有传值，这时应有一个默认页码设置为1
		$page = I('get.page') !== null ? I('get.page') : 1;
		$where = array('category_id' => $ci);      //商品分类编号为ci值

		//调用模型类的分页方法获取处理后的page、pages、limit，返回的是数组形式，按这个顺序0、1、2依次获取，参数10为每页显示商品条数
		$pageInfo = $model->paging(8, $page, $where);

		//获取最新上架商品信息,按照入库时间来排，并添加分页显示
		$where = isset($where['category_id']) ? $where : '';
		$goodsInfo = $model->limit($pageInfo[2])->order('goods_indate desc')->where($where)->select();

		//显示分页视图，创建分页类对象
		$p = new \Admin\Components\Pager($pageInfo[0], $pageInfo[1]); //直接通过new组件类对象传递page和pages，初始化该对象，返回生成的分页视图
		$paging = $p->fenye($where);              //调用该组件类的fenye方法，获得返回的分页视图字符串
		$this->assign("paging", $paging);  //将该视图作为变量传递到index视图中
		//将获取到的商品信息，传给视图
		$this->assign("goodsInfo", $goodsInfo);
		$this->display();
	}

	//商品查询功能
	public function query()
	{
		//检查管理员登录信息
		$this->checkAdmin();
		//获取传递的商品名称信息
		$goodsName = I('goods_name');
		$where = array('goods_name' => array("like", '%' . $goodsName . '%')); //构建商品名称模糊查询条件
		$model = D('goods');   //实例化模型类对象	
		$goodsInfo = $model->where($where)->select();  //获取模糊查询商品信息
		$this->assign("goodsInfo", $goodsInfo);
		$this->display();
	}

	//添加商品
	public function add()
	{
		//检查管理员登录信息
		$this->checkAdmin();
		if (IS_POST) {
			$goodsinfo = I('post.');
			//查询该商品是否存在
			$model = M('Goods');
			if ($model->where(array('goods_name' => $goodsinfo['goods_name']))->find()) {
				$this->error("商品已存在，无法添加！", U('?m=admin&c=goods&a=index'));
				die;
			}
			$where = array(
				'goods_brand' => $goodsinfo['goods_brand'],
				'goods_name' => $goodsinfo['goods_name'],
				'goods_price' => $goodsinfo['goods_price'],
				'goods_descript' => $goodsinfo['goods_descript'],
				'goods_status' => $goodsinfo['goods_status'],
				'goods_address' => $goodsinfo['goods_address'],
				'goods_qty' => $goodsinfo['goods_qty'],
				'category_id' => $goodsinfo['category_id'],
				'pic_url' => 'idel.jpg', // 使用默认图片，没得处理
			);
			if (!empty($model->add($where))) {
				$this->success("商品添加成功！", U('?m=admin&c=goods&a=index'));
				die;
			}
			$this->error("商品无法添加！", U('?m=admin&c=goods&a=index'));
			die;
		}
		$categoryInfo = M('Category')->select();
		$this->assign('categoryInfo', $categoryInfo);
		$this->display();
	}

	//编辑商品信息
	public function update()
	{
		//检查管理员登录信息
		$this->checkAdmin();
		if (IS_POST) {
			$goodsinfo = I('post.');
			//查询该商品是否存在
			$model = M('Goods');
			$where = array('goods_id' => $goodsinfo['goods_id']);
			$data = array(
				'goods_brand' => $goodsinfo['goods_brand'],
				'goods_name' => $goodsinfo['goods_name'],
				'goods_price' => $goodsinfo['goods_price'],
				'goods_descript' => $goodsinfo['goods_descript'],
				'goods_status' => $goodsinfo['goods_status'],
				'goods_address' => $goodsinfo['goods_address'],
				'goods_qty' => $goodsinfo['goods_qty'],
				'category_id' => $goodsinfo['category_id'],
				'pic_url' => $goodsinfo['pic_url'],
			);
			if ($model->where($where)->save($data) !== false) {
				$this->success("商品信息修改成功！", U('?m=admin&c=goods&a=index'));
				die;
			}
			$this->error("商品信息无法修改！", U('?m=admin&c=goods&a=index'));
			die;
		}
		$goods_id = I('get.gi');  //获取商品编号
		//查询该商品是否存在
		$model = M('goods');
		//如果存在
		if ($goods_info = $model->where(array('goods_id' => $goods_id))->find()) {
			$categoryInfo = M('Category')->select();  //查询分类信息
			$this->assign('categoryInfo', $categoryInfo);
			$this->assign('goodsInfo', $goods_info);

			$this->display();
			die;
		}
		$this->error("无该商品信息！！！！", U('?m=admin&c=goods&a=index'));
	}

	//添加产品图片
	public function addImg()
	{
		//检查管理员登录信息
		$this->checkAdmin();
		$model = D('Goods');   //实例化商品模型
		if (IS_POST)  //如果有图片上传
		{
			//判断文件有没有被上传到服务器缓存目录
			if ($_FILES['pic_url']['tmp_name'] != '') {
				$upload = new \Think\Upload(); // 实例化上传类
				$upload->maxSize   =     3145728; // 设置附件上传大小
				$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
				// 设置附件上传根目录，如果服务器中没有这个目录，必须新建文件夹设置，否则无法上传文件
				$upload->rootPath  =    './Public/';   // 设置附件上传根目录
				$upload->savePath  =     './images/goods/'; // 设置附件上传（子）目录,
				$upload->autoSub = false;   // 不自动使用子目录保存上传文件
				$upload->saveName = '';    // 使用原文件名，不生成uniqid，如test1.jpg
				$upload->replace = true;    //允许同名文件覆盖
				// 上传文件 
				$info   =   $upload->upload();
				if (!$info) { // 上传错误提示错误信息
					$this->error($upload->getError());
				} else {
					// 上传成功
					$goods_id = I('post.goods_id');  //获取商品编号
					$data = array('pic_url' => $_FILES['pic_url']['name']);
					$where = array('goods_id' => $goods_id);
					$model->where($where)->save($data);
					$this->success('上传图片成功！', U('?m=admin&c=goods&a=index'));
					die;
				}
				//打印上传完成后返回的上传文件的信息
				//dump($info);
			}
			$this->error('上传图片失败成功！', U('?m=admin&c=goods&a=addImg&gi=' . $goods_id));
			die;
		}
		//获取商品编号
		$good_id = I('get.gi');
		//查询该商品信息
		$good_pic = $model->field('pic_url')->where(array('goods_id' => $good_id))->find();
		if ($good_pic) {
			$this->assign('pic_url', $good_pic['pic_url']);
			$this->assign('goods_id', $good_id);
			$this->display();
			die;
		} else {
			if ($model->where(array('goods_id' => $good_id))->save(array('pic_url' => 'idel.jpg'))) {
				$this->error("暂无图片信息，已更改为默认图片", U('?m=admin&c=goods&a=index'));
			}
		}
	}

	//还原回收商品
	public function regain()
	{
		//检查管理员登录信息
		$this->checkAdmin();
		$dpi = I('dpi');  //获取回收商品编号
		$modelDump = M('dump');
		$goodsInfo = $modelDump->where(array('dump_id' => $dpi))->find();  //获取该回收商品信息
		$modelGoods = D('goods');          //实例化商品模型类对象
		unset($goodsInfo['dump_id']);   //回收商品编号删除
		if ($modelGoods->add($goodsInfo))   //还原该商品成功
		{
			$modelDump->where(array('dump_id' => $dpi))->delete();  //回收站该商品信息删除
			$this->success("商品已还原", U('?m=admin&c=goods&a=index'));
			die;
		}
		$this->error("商品还原失败", U('?m=admin&c=dump&a=showList'));
		die;
	}
}
